package com.atguigu.gulimall.seckill.controller;

import com.atguigu.common.utils.R;
import com.atguigu.gulimall.seckill.service.SeckillService;
import com.atguigu.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    /**
     * 返回当前时间可以秒杀的商品信息
     */

    @GetMapping("/currentSeckillSkus")
    @ResponseBody
    public R currentSeckillSkus(){
       List<SeckillSkuRedisTo> list = seckillService.getcurrentSeckillSkus();
       return R.ok().put("list",list);
    }

    @GetMapping("/sku/seckill/{skuId}")
    @ResponseBody
    public R getSkuSeckillInfo(@PathVariable("skuId") Long skuId){
      SeckillSkuRedisTo to =  seckillService.getSkuSeckillInfo(skuId);
      return R.ok().put("to",to);
    }


    @GetMapping("/kill")
   public String seckill(@RequestParam("killId") String killId, @RequestParam("code") String code
    , @RequestParam("num") Integer num, Model model){

       String orderSn= seckillService.kill(killId,code,num);
       model.addAttribute("orderSn",orderSn);
        System.out.println("1111111111");
       return "success";

    }
}
